package com.krystal.controller;

import com.krystal.bean.Result;
import com.krystal.mapper.CovidMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.FastDateFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @author Krystal1
 * @date 2021/4/26 -13:04
 * 用来接收前端数据请求的Controller
 */
@Controller  //=@Controller+@ResponseBody  表示该类是springboot的一个Controller，返回的数据为json格式
@RequestMapping("covid")
@Slf4j
public class Covid19Controller {

    @Autowired
    private  CovidMapper covidMapper;

    /**
     * 接收前端请求返回全国疫情汇总数据
     * */
    @ResponseBody
    @RequestMapping("getNationalData")
    public Result getNationalData(){
        //System.out.println("接收到前端发起的获取数据的请求，后续需要查询mysql将数据返回");
        String datetime = FastDateFormat.getInstance("yyyy-MM-dd").format(System.currentTimeMillis());
        Map<String, Object> data = covidMapper.getNationalDataNew();
        data.put("datetime",datetime);
        data.put("currentConfirmedCount", data.get("nowConfirm"));
        data.put("confirmedCount", data.get("confirm"));
        data.put("suspectedCount",data.get("suspect"));
        data.put("curedCount", data.get("heal"));
        data.put("deadCount", data.get("dead"));
        Result result = Result.success(data);
//        log.info("result:{}",result);
        return result;
    }


    //getNationalMapData
    /**
     * 查询全国各省份累计确诊数据并返回
     * */
    @ResponseBody
    @RequestMapping("getNationalMapData")
    public Result getNationalMapData(){

        String datetime = FastDateFormat.getInstance("yyyy-MM-dd").format(System.currentTimeMillis());
        List<Map<String, Object>> data = covidMapper.getNationalMapData(datetime);
        Result result = Result.success(data);
//        log.info("result:{}",result);
        return result;
    }

    //getCovidTimeData
    /**
     *查询全国每一天的疫情数据并返回  3.27
     **/
    @ResponseBody
    @RequestMapping("getCovidTimeData")
    public Result getCovidTimeData(){

        List<Map<String, Object>> data=covidMapper.getCovidTimeData();
        Result result = Result.success(data);
//        log.info("result:{}",result);
        return result;
    }

    /**
     * 查询全国近两个月的疫情数据并返回 代替 ↑ 的自2019年的数据                修改  3.27
     * @return Result
     */
    @ResponseBody
    @RequestMapping("getTwoMonthsData")
    public Result getTwoMonthsData(){

        List<Map<String, Object>> twoMonthsData = covidMapper.getTwoMonthsData();
        log.info("返回了近两个月的数据！");
        return Result.success(twoMonthsData);
    }


    //getCovidIncr
    /**
     * 查询全国最近一个月以来新增的数据并返回
     * */
    @ResponseBody
    @RequestMapping("getCovidIncr")
    public Result getCovidIncr(){

        int dataCount = covidMapper.getDataCount();         //得到表covid19_1的数据量
        int dataIncr = dataCount-31;
        List<Map<String, Object>> data=covidMapper.getCovidIncrData(dataIncr);
        Result result = Result.success(data);
//        log.info("result:{}",result);
        return result;
    }

    //getCovidImportData
    /**
     * 查询各省份境外输入病例数量
     **/
    @ResponseBody
    @RequestMapping("getCovidImportData")
    public Result getCovidImportData(){
        String datetime = FastDateFormat.getInstance("yyyy-MM-dd").format(System.currentTimeMillis());
        List<Map<String, Object>> data=covidMapper.getCovidImportData(datetime);
//        log.info("result:{}", Result.success(data));
        return Result.success(data);
    }

    /**
     * 查询近一个月里面的境外输入趋势
     * */
    @ResponseBody
    @RequestMapping("getCovidImportChina")
    public Result getCovidImportChinaDay(){

        int dataCount = covidMapper.selectDataChinaDay();         //得到表covid19_0的数据量
        int dataFirst = dataCount-31;
        List<Map<String, Object>> data = covidMapper.getChinaDayImport(dataFirst);
        Result result = Result.success(data);
//        log.info("result:{}",result);
        return result;
    }

    /**
    * 查询这一天的中国疫情数据 作为饼图
     * **/
    @ResponseBody
    @RequestMapping("getChinaData")
    public Map<String,Object> getChinaData(){

        Map<String,Object> map= covidMapper.getChinaData();
//        log.info("result:{}",map);
        log.info("这天的中国疫情数据（作为饼图）",map);
        return map;
    }
}
